package hu.myai.controller;

import hu.myai.model.db.AxHistory;
import hu.myai.util.Analyst;
import hu.myai.util.DatabaseUtil;
import hu.myai.util.DownloadUtil;
import hu.myai.util.Util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.joone.log.ILogger;
import org.joone.log.LoggerFactory;

public class Suggestion {

	private static final ILogger log = LoggerFactory.getLogger(Suggestion.class);

	private Util util = new Util();

	private DatabaseUtil dbUtil = new DatabaseUtil();

	private DownloadUtil downloadUtil = new DownloadUtil();

	@SuppressWarnings("unchecked")
	public void getSuggestion(Date date) {

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

		/** Gyertyák egymás felett */
		List<AxHistory> history = dbUtil.entityManager
				.createNativeQuery(
						"select h_new.* from ax_history h_new, "
								+ "	(select distinct ho.symbol, max(hn.close/ho.close) as per from ax_history ho, ax_history hn  "
								+ "		where hn.parent = ho.id  "
								+ "         and hn.low = hn.open and hn.open < hn.close"
								+ "			and (ho.high = hn.open or ho.close = hn.open) "
								+ "			and hn.book_maxbuy >= hn.close and hn.date between :intFrom and :intTo "
								+ "			and ho.volume * ho.close > 2000 " + "			and hn.volume * hn.close > 2000  "
								+ "		group by ho.symbol) h_old  "
								+ "where h_old.symbol = h_new.symbol and h_new.date = :currentDate "
								+ "	and h_new.volume * h_new.close > 2000 order by h_old.per desc limit 100",
						AxHistory.class).setParameter("intFrom", util.addDays(date, -33)).setParameter("intTo",
						util.addDays(date, -3)).setParameter("currentDate", date).getResultList();

		if (history.size() == 0) {
			log.error("Error - no suggestion b/c no estimation record found, date may wrong");
			return;
		}

		for (AxHistory histRecord : history) {

			String dateString = formatter.format(date);

			// System.out.println(dateString + " symbol:" +
			// estRecord.get_symbol() + " est1: " + est1 + " est2: "
			// + est2 + " est3: " + est3 + " err: " + err);

			downloadUtil.getPic(dateString, histRecord.getSymbol(), "");
		}
	}

	@SuppressWarnings("unchecked")
	public void getSuggestion2(Date date) {

		/** A harmadik gyertya kiugrik felvele volummal */
		List<AxHistory> history = dbUtil.entityManager.createNativeQuery(
				"select h_new.* from ax_history h_new, "
						+ "	(select distinct h3.symbol, max(h3.volume/h2.volume) as per "
						+ "	from ax_history h1, ax_history h2,ax_history h3 "
						+ "	where h3.parent = h2.id  and h2.parent = h1.id "
						+ "	 and (h3.open<h3.close and h3.low < h3.open and h3.high > h3.close or "
						+ "	      h3.open>h3.close and h3.low < h3.close and h3.high > h3.open) "
						+ "	 and h3.low >= h2.high and h3.low >= h1.high " + "	 and h1.volume * h1.close > 2000 "
						+ "	 and h2.volume * h2.close > 2000 " + "	 and h3.volume > h1.volume * 2.8 "
						+ "	 and h3.volume > h2.volume * 2.8 " + "	 and h3.date between :intFrom and :intTo "
						+ "	group by h3.symbol) h_old "
						+ "where h_old.symbol = h_new.symbol and h_new.date = :currentDate "
						+ "and h_new.volume * h_new.close > 2000 order by h_old.per desc limit 100", AxHistory.class)
				.setParameter("intFrom", util.addDays(date, -33)).setParameter("intTo", util.addDays(date, -3))
				.setParameter("currentDate", date).getResultList();

		log.info("Found: " + history.size());

		if (history.size() == 0) {
			log.error("Error - no suggestion2 b/c no estimation record found, date may wrong");
			return;
		}

		for (AxHistory estRecord : history) {

			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			String dateString = formatter.format(date);

			System.out.println(dateString + "  symbol:" + estRecord.getSymbol());

			downloadUtil.getPic(dateString, estRecord.getSymbol(), formatter.format(estRecord.getDate()));
		}
	}

	/**
	 * preEvaluation done for all symbols and download of image if symbol is
	 * suggested for trade
	 */
	public void getSuggestion3(int mode) {

		Analyst analyst = new Analyst();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

		List<String> symbolList = downloadUtil.getASXCompanyList();

		for (String symbol : symbolList) {
			Boolean suggested = analyst.preEvaluation(symbol, mode);
			AxHistory lastSaved;
			if (suggested) {
				lastSaved = dbUtil.getLastHistory(new Date(), symbol);
				String dateString = formatter.format(lastSaved.getDate());
				downloadUtil.getPic(dateString, symbol, "");
			}
		}
	}

	public void getSuggestion4() throws InterruptedException {

		Analyst analiAnalyst = new Analyst();
		List<Thread> threadList = new ArrayList<Thread>();

		List<String> symbolList = downloadUtil.getASXCompanyList();

		int i = 1;
		for (String symbol : symbolList) {
			threadList.add(analiAnalyst.start(symbol, 24));
			if (i % 10 == 0) {
				threadList.get(i - 1).join();
			}
			i++;
		}

		analiAnalyst.finish();
		threadList.get(threadList.size() - 1).join();

		util.writeIndexHtml(util.createIndexHtml(Analyst.goodForTrade).toString());
	}
}
